package cn.edu.neusoft.food229.model;

import android.content.Intent;
import android.widget.Toast;

import cn.edu.neusoft.food229.activity.LoginActivity;
import cn.edu.neusoft.food229.activity.MainActivity;
import cn.edu.neusoft.food229.bean.LoginBean;
import cn.edu.neusoft.food229.iface.Loginiface;
import cn.edu.neusoft.food229.listener.LoginListener;
import cn.edu.neusoft.food229.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class LoginModel implements Loginiface{
    private Retrofit retrofit;
    public LoginModel(){
       retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getLoginResult(String username, String userpass, final LoginListener loginListener) {
        UserService userService=retrofit.create(UserService.class);
        Call<LoginBean> call
                = userService.login(username, userpass);

        call.enqueue(new Callback<LoginBean>() {

            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if (response.isSuccessful() && response.body()!= null && response.body().getUserid()!=  null) {
                    if(response.body().getUserid().equals("0"))
                    {
                        loginListener.onFail("没有该用户！！！");
                    }
                    else
                       // loginListener.onFail("登录失败！");
                    loginListener.onResponce(response.body());

                }
                else {
                    loginListener.onFail("未知错误");
                }
            }
            public void onFailure(Call<LoginBean> call, Throwable t) {
                System.out.println("--网络错误");
                loginListener.onFail("网络错误");
            }
        });
    }
}
